package com.eascs.web.p2p.investor.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.common.entity.BannerSetEntity;
import com.eascs.p2p.common.entity.InvestObjExtEntity;
import com.eascs.p2p.common.entity.InvestObjectEntity;
import com.eascs.p2p.common.entity.InvestorEntity;
import com.eascs.p2p.common.entity.InvestorRecordEntity;
import com.eascs.p2p.common.entity.InvestorWalletEntity;
import com.eascs.p2p.common.service.IBannerService;
import com.eascs.p2p.common.service.IInvestObjectService;
import com.eascs.p2p.common.service.IInvestorRecordService;
import com.eascs.p2p.common.service.InvestorService;
import com.eascs.p2p.common.vo.InvestObjectVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.p2p.common.vo.TransactionRecordVO;
import com.eascs.p2p.common.wallet.service.InvestorWalletService;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.p2p.common.controller.BaseMasterViewController;
import com.eascs.web.p2p.common.entity.vo.PageResultVO;
import com.eascs.web.p2p.member.controller.LoginRestController;
import com.eascs.web.p2p.member.untils.MobileUtils;

@RequestMapping("/investor")
@Controller
public class InvestorController extends BaseMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(InvestorController.class);

	@Reference
	IInvestObjectService investObjectService;
	@Reference
	InvestorWalletService investorWalletService;
	@Reference
	InvestorService investorService;
	@Reference
	IInvestorRecordService investorRecordService;
	@Reference
	IBannerService bannerService;

	@Value("${web.banner.code}")
	String bannerCode;

	@RequestMapping(value = "/uc/bet/details")
	public String buyDetails(HttpServletRequest request, String investObjId) {
		try {
			ModelResult<InvestorEntity> modelResult = LoginRestController.isLogin(request);
			InvestorEntity Investor = modelResult.getModel();
			ModelResult<InvestObjectEntity> result = investObjectService.findInvestObjectDetail(investObjId);
			if (result.isSuccess()) {
				InvestObjectEntity investObject = result.getModel();
				BigDecimal dropMoney = investObject.getLoanAmount().subtract(investObject.getInvestedAmount());
				ViewContext.put("investObject", investObject);
				ViewContext.put("dropMoney", dropMoney.setScale(2, BigDecimal.ROUND_HALF_UP));
				InvestorWalletEntity wallet = investorWalletService.queryInvestorWallet(Investor.getId()).getModel();
				ViewContext.put("balance", wallet.getCurrentBalance().setScale(2, BigDecimal.ROUND_HALF_UP));
			}
			return "/investor/betDetails";

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {
		InvestObjectVO vo = new InvestObjectVO();
		ModelResult<PageResult<InvestObjectEntity>> result = investObjectService.findFontPageInvestObjectEntity(vo);
		if (result.isSuccess()) {
			ViewContext.put("datas", result.getModel().getData());
		} else {
			logger.error("数据异常", result.getErrMsg());
			ViewContext.put("datas", "");
		}

		ModelResult<InvestorEntity> modelResult = LoginRestController.isLogin(request);
		if (modelResult.isSuccess()) {
			InvestorEntity investor = modelResult.getModel();
			ViewContext.put("investor", investor);
		}

		ModelResult<List<BannerSetEntity>> bannerResult = bannerService.getBannerSetEntity(bannerCode);
		if (bannerResult.isSuccess()) {
			List<BannerSetEntity> banners = bannerResult.getModel();
			ViewContext.put("banners", banners);
		}

		return "/investor/index";
	}

	@RequestMapping(value = "/objectList")
	public String objectList(HttpServletRequest request) {
		ModelResult<InvestorEntity> modelResult = LoginRestController.isLogin(request);
		if (modelResult.isSuccess()) {
			InvestorEntity investor = modelResult.getModel();
			ViewContext.put("investor", investor);
		}
		return "/investor/investObjectList";
	}

	@RequestMapping(value = "/details")
	public String manager(String id) {
		ModelResult<InvestObjectEntity> result = investObjectService.findInvestObjectDetail(id);
		if (result.isSuccess()) {
			ViewContext.put("datas", result.getModel());
		} else {
			logger.error("数据异常", result.getErrMsg());
			ViewContext.put("datas", "");
		}
		BaseRestResult.refresh();
		return "/investor/details";
	}
	
	@RequestMapping(value = "/investObject/description")
	public String investObjectDes(String investObjId) {
		 ModelResult<InvestObjExtEntity> objExt = investObjectService.findInvestObjExtEntityDetail(investObjId);
		ModelResult<InvestObjectEntity> obj = investObjectService.findInvestObjectDetail(investObjId);
		if (objExt.isSuccess() && obj.isSuccess()) {
			ViewContext.put("objExt", objExt.getModel());
			ViewContext.put("obj", obj.getModel());
		} else {
			logger.error("数据异常", objExt.getErrMsg());
			ViewContext.put("objExt", "");
			ViewContext.put("obj", "");
		}
		BaseRestResult.refresh();
		return "/investor/investObjectDes";
	}

	@RequestMapping(value = "/uc/bet/buyInvest")
	public String buyInvest(HttpServletRequest request, String investObjId, String investMoney) {
		if (investObjId.isEmpty() || investMoney.isEmpty()) {
			logger.error("下单失败，参数为空,investObjId:{}, investMoney:{}", investObjId, investMoney);
			return "/investor/betFail";
		}
		ModelResult<InvestorEntity> modelResult = LoginRestController.isLogin(request);
		InvestorEntity investor = modelResult.getModel();
		ModelResult<Boolean> reuslt = investorService.buyInvest(investor, investObjId, new BigDecimal(investMoney));
		if (reuslt.isSuccess() && reuslt.getModel()) {
			return "/investor/betSucceed";
		}
		ViewContext.put("errorMsg", reuslt.getErrMsg());
		return "/investor/betFail";
	}

	@RequestMapping(value = "/html/investObjectRecord", method = RequestMethod.GET)
	public String investObjectRecordPage(HttpServletRequest request, String investObjId) {
		ViewContext.put("investObjId", investObjId);
		return "/investor/investRecord";
	}

	@RequestMapping(value = "/uc/html/investorRecord", method = RequestMethod.GET)
	public String investorRecordPage(HttpServletRequest request) {
		ModelResult<InvestorEntity> modelResult = LoginRestController.isLogin(request);
		return "/investor/myInvestRecord";
	}

	/**
	 * 标的所有投资记录
	 */
	@RequestMapping(value = "/ajax/investObjectRecord", method = RequestMethod.GET)
	public String investObjectRecord(String objectId, PageResultVO page) {
		if (StringUtils.isBlank(objectId)) {
			return "";
		}
		InvestorRecordEntity investRecord = new InvestorRecordEntity();
		investRecord.setObjectId(objectId);
		ModelResult<PageResult<TransactionRecordVO>> pageResult = investorRecordService
				.queryPageInvestRecord(investRecord, page.getPageSize(), page.getPageIndex(), -1);
		if (pageResult.isSuccess()) {
			for (TransactionRecordVO vo : pageResult.getModel().getData()) {
				String account = vo.getInvestorAccount();
				if (MobileUtils.validateMobile(account)) {
					account = MobileUtils.closedMobile(account);
					vo.setInvestorAccount(account);
				}
			}
			ViewContext.put("result", pageResult.getModel());
		}
		return "/investor/investObjectRecordDIV";
	}

	/**
	 * 投资人已投标的投资记录
	 */
	@RequestMapping(value = "/uc/ajax/investorRecord", method = RequestMethod.GET)
	public String investorRecord(HttpServletRequest request, PageResultVO page, int objectStatus) {
		ModelResult<InvestorEntity> modelResult = LoginRestController.isLogin(request);
		InvestorEntity investor = modelResult.getModel();
		InvestorRecordEntity investRecord = new InvestorRecordEntity();
		investRecord.setInvestorId(investor.getId());
		ModelResult<PageResult<TransactionRecordVO>> pageResult = investorRecordService
				.queryPageInvestRecord(investRecord, page.getPageSize(), page.getPageIndex(), objectStatus);
		if (pageResult.isSuccess()) {
			ViewContext.put("result", pageResult.getModel());
		}
		return "/investor/myInvestRecordDIV";
	}
	
	
	@RequestMapping("/html/disclaimer")
	public String disclaimer() {
		return "/investor/disclaimer";
	}
	
	@RequestMapping("/html/agreement")
	public String agreement() {
		return "/investor/agreement";
	}

	@RequestMapping("/html/faq")
	public String faq() {
		return "/investor/faq";
	}
	
	@RequestMapping("/html/aboutUs")
	public String aboutUs() {
		return "/investor/aboutUs";
	}
}
